package com.ls.sync;

//创建一个资源类,定义属性和操作方法
class Ticket {
    private int number = 30;

    //卖票
    public synchronized void sale() {
        if (number > 0) {
            System.out.println(Thread.currentThread().getName() + ":卖出:" + number-- + " 剩余:" +  number);
        }

    }
}

public class SaleTicket {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        Thread aa = new Thread(new Runnable(){
            @Override
            public void run() {
                //卖票
                for (int i = 0; i < 40; i++) {
                    ticket.sale();
                }
            }
        }, "aa");

        Thread bb = new Thread(new Runnable(){
            @Override
            public void run() {
                //卖票
                for (int i = 0; i < 40; i++) {
                    ticket.sale();
                }
            }
        }, "bb");

        Thread cc = new Thread(new Runnable(){
            @Override
            public void run() {
                //卖票
                for (int i = 0; i < 40; i++) {
                    ticket.sale();
                }
            }
        }, "cc");

        aa.start();
        bb.start();
        cc.start();

    }
}
